import header from "./header.js";
import banner from "./banner.js";
import aside from "./aside.js";
import loaddata from "./loaddata.js";
import render from "./render.js";
import goBack from "./goTop.js";
import cutDown from "./cutDown.js";
export default function(){
    var ls = localStorage.getItem("user");
    if(ls !== null){
        var la = JSON.parse(ls);
        var html = "";
        $.each(la,function(index,item){
            html = `ip${item.user_phone}`;
        })
        $(".login_id").html(html);
        $(".login_id").on("click",$.proxy(function(){
            if(window.confirm("是否退出登录")){
                $(".login_id").html("登录");
                $(".login_id").removeAttr("href");
            }
        },this));
        // $(".login_id").attr("href","http://localhost/VSCode/JuanPi_web_project/src/login.html");
    }
    var goods_list = $(".goods-list");
    var timer = null;
    cutDown().init(2019,8,18);
    banner();
    header().init();
    aside().init();
    goBack().init();
    loaddata()
    .done(function(res){
        // 商品页渲染
        var html = render().init(res, "goods_list");
        goods_list.html(html);
        var img  = $(".goods-list-img");
        $(window).scroll(function(){
            clearTimeout(timer);
            timer = setTimeout(function(){
                var scroll = $("html,body").scrollTop();
                showAll(scroll,img);
            },100);
        })
        // detail().init();//储存的数据 
        this.cartNum = $(".cartnum");
        var ls = localStorage.getItem("goods");
        var la = JSON.parse(ls === null ? "[]" : ls);
        var sum = 0;
        la.forEach( function(item){
            sum += Number(item.count);
        })
        $(this.cartNum).html(sum);
    })
    function showAll(data,img){
        $.each(img,function(index,item){
            var src = $(item).attr("real-src");
            if(data >= $(item).offset().top - 450){    
                $(item).attr("src",src);
            }
        })
    }
}